//
//  WCISegmentView.m
//  WCImApp
//
//  Created by macMini on 2020/6/28.
//  Copyright © 2020 WCImApp. All rights reserved.
//

#import "WCISegmentView.h"

@implementation WCISegmentView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithLightColorStr:@"EBEBED" DarkColor:@"242629"];
        ViewRadius(self, 17);
    }
    return self;
}
- (NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [[NSMutableArray alloc]init];
    }
    return _btnArray;
}
- (void)btnClick:(UIButton *)btn{
    if (self.lastBtn != btn) {
        self.lastBtn.selected = NO;
        self.lastBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        self.lastBtn = btn;
        self.currentIndex = btn.tag-100;
        btn.selected = YES;
        btn.titleLabel.font = WCIFontMedium(14);
        if ([self.delegate respondsToSelector:@selector(segmentViewWithSelectIndex:)]) {
            [self.delegate segmentViewWithSelectIndex:btn.tag-100];
        }
        
        CGFloat btnWidth = (self.width-2*self.btnArray.count-2)/self.btnArray.count;
        [UIView animateWithDuration:0.3 animations:^{
            self.moveBgView.frame = CGRectMake(2+(btnWidth+2)*self.currentIndex, 2, btnWidth, 30);
        }];
    }
}
//将某个按钮选中
- (void)setSegmentWithIndex:(NSInteger)index{
    if (index < self.btnArray.count) {
        UIButton *btn = self.btnArray[index];
        [self btnClick:btn];
    }
}
- (WCISegmentView *)setSegmentTitleArray:(NSArray *)titleArray andDelegate:(id<MOSegmentViewDelegate>)delegate initIndex:(NSInteger)index {
    self.delegate = delegate;
    self.currentIndex = index;

    //避免按钮视图重复
    for (int i = 0; i < self.btnArray.count; i++) {
        UIButton *btn = [self viewWithTag:100+i];
        [btn removeFromSuperview];
    }
    [self.btnArray removeAllObjects];
    [self.moveBgView removeFromSuperview];
    
    CGFloat btnWidth = (self.width-2*titleArray.count-2)/titleArray.count;
    self.moveBgView.frame = CGRectMake(2+(btnWidth+2)*index, 2, btnWidth, 30);
    [self addSubview:self.moveBgView];
    
    for (int i = 0; i < titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        ViewRadius(btn, 15);
        btn.tag = 100+i;
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"0x6d6f76"] forState:UIControlStateNormal];
        [btn setTitleColor:mp_main_text_color forState:UIControlStateSelected];
        btn.frame = CGRectMake(2+(btnWidth+2)*i, 2, btnWidth, 30);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        if (index == i) {
            self.lastBtn = btn;
            btn.selected = YES;
            btn.titleLabel.font = WCIFontRegular(14);
        }else{
            btn.selected = NO;
            btn.titleLabel.font = [UIFont systemFontOfSize:14];;
        }
        [self.btnArray addObject:btn];
    }
    return self;
}
- (UIView *)moveBgView{
    if (!_moveBgView) {
        _moveBgView = [[UIView alloc]init];
        ViewRadius(_moveBgView, 15);
        _moveBgView.backgroundColor = assist_voice_gray_color;
    }
    return _moveBgView;
}
@end
